package com.efounder.form.comp.commodity;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;

import java.util.List;

/**
 * Created by XinQing on 2016/10/14.
 */

public class CommodityDataManager {


    /**
     * 当前类别Item总数。标题也需要占用一个Item
     * @return
     */
    public static int getItemCount(EFDataSet dataSet){
//        return dataSet.getRowSetArray().size() + 1;
        return dataSet.getRowSetArray()==null ? 0 + 1: dataSet.getRowSetArray().size() + 1;
    }


    /**
     *  获取Item内容
     * @param position
     * @return
     */
    public static EFRowSet getItem(EFDataSet dataSet,int position) {
        // 标题排在第一位
        if (position == 0) {
            return dataSet.getParentRowSet();
        }  else  {
            dataSet.goToRow(position - 1);
            return (EFRowSet) dataSet.getRowSetList().get(position - 1);
        }
    }

    public static boolean isDataSetChecked(EFDataSet dataSet){
        boolean isChecked = true;
        List<EFRowSet> rowSetList = dataSet.getRowSetArray();
        if (rowSetList == null) return false;
        for (EFRowSet rowSet:rowSetList) {
            if (!rowSet.isRowSelected()){
                isChecked = false;
                break;
            }
        }
        return  isChecked;
    }
    public static void setDataSetChecked(EFDataSet dataSet,boolean isChecked){
        List<EFRowSet> rowSetList = dataSet.getRowSetArray();
        if (rowSetList != null){
            for (EFRowSet rowSet:rowSetList) {
                rowSet.setRowSelected(isChecked);
            }
        }
    }

    public static int getPosition(List<EFDataSet> dataSetList,EFDataSet dataSet){
        int position = 0;
        for (EFDataSet tempDataSet:dataSetList) {
            if (dataSet == tempDataSet){
                return position;
            }else {
                position += getItemCount(tempDataSet);
            }
        }

        return 0;
    }

}
